package edu.biwu.session01;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

/**
 * 获取Session
 * HttpServletRequest中的方法:
 *     public HttpSession getSession():
 *          如果当前服务端没有Session,那么就在服务端新建一个Session
 *          如果当前服务端有这个Session,那么就返回这个Session
 * HttpSession中的方法:
 *    public Object getAttribute(String name):从session中根据name取出对应的值
 * */
@WebServlet("/session/get")
public class GetSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取原有的session
        HttpSession session = request.getSession();
        //2.取出session中的数据
        response.getWriter().write(session.getAttribute("id").toString());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
